package com.cyl.XmlDemo.web;

import java.security.Principal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;

import com.cyl.XmlDemo.entity.Room;
import com.cyl.XmlDemo.entity.Shout;
import com.cyl.XmlDemo.entity.StompClient;
import com.cyl.XmlDemo.service.MessageService;

//@Controller
public class MessageController {

	private static final Logger logger = LoggerFactory.getLogger(MessageController.class);
	
	@Autowired
	private MessageService msgService;
	
	@Autowired
	private StompClient stompClient;
	
	@Autowired
	private Room room;
	
	/**
	 * 信息中转
	 * @param shout
	 * @param principal
	 */
//	@MessageMapping("/topic/transfer-msg")
	public void transfer(Shout shout,Principal principal){
		logger.debug(shout.getData().toString());
		shout.setCaller(principal.getName());
		msgService.transferMetaData(shout);
	}
	
	@MessageMapping("/topic/room")
	public void room(Principal principal){
		int num = room.size();
		Shout shout = new Shout();
		if(num == 1){
			shout.setCmd("wait");
			msgService.sendMsg(principal.getName(), shout);
		}else if(num >= 2 && num <= 5){
			shout.setCmd("join");
			msgService.sendCMD(principal.getName(), shout);
		}else{
			shout.setCmd("full");
			msgService.sendMsg(principal.getName(), shout);
		}
		logger.debug("/topic/room 被触发");
	}
}
